// pages/doctor-detail/doctor-detail.js
Page({
	data: {
		doctor: null,
		otherDoctors: [],
		department: '',
		subDepartment: '',
		selectedCampus: '湘南院区' // 添加院区信息
	},

	onLoad(options) {
		console.log('医生详情页面加载', options)
		
		// 接收传递的参数
		if (options.doctor && options.title && options.specialty && options.fee) {
			const doctor = {
				name: options.doctor,
				title: options.title,
				specialty: options.specialty,
				fee: options.fee,
				department: options.department || '',
				subDepartment: options.subDepartment || '',
				campus: options.campus || '湘南院区' // 接收院区参数
			}
			
			// 生成医生详细信息
			this.generateDoctorDetail(doctor)
			
			this.setData({
				department: options.department || '',
				subDepartment: options.subDepartment || '',
				selectedCampus: options.campus || '湘南院区' // 设置院区
			})
		}
	},

	// 生成医生详细信息
	generateDoctorDetail(doctor) {
		// 根据医生姓名生成不同的标签和简介
		const doctorHash = this.hashCode(doctor.name)
		const tags = this.generateDoctorTags(doctor.name, doctorHash)
		const introduction = this.generateDoctorIntroduction(doctor.name, doctor.specialty, doctorHash)
		const schedules = this.generateDoctorSchedules(doctor.name, doctorHash)
		const otherDoctors = this.generateOtherDoctors(doctor.name, doctorHash)
		
		this.setData({
			doctor: {
				...doctor,
				tags: tags,
				introduction: introduction,
				schedules: schedules
			},
			otherDoctors: otherDoctors
		})
	},

	// 生成医生标签
	generateDoctorTags(doctorName, hash) {
		const allTags = [
			'糖尿病', '高血压', '心脏病', '肾病', '肝病', '胃病', '肠病', '肺病',
			'脑病', '骨科', '妇科', '儿科', '皮肤科', '眼科', '耳鼻喉科',
			'肿瘤科', '血液科', '免疫科', '内分泌科', '神经科', '心理科'
		]
		
		const selectedTags = []
		const numTags = (hash % 3) + 2 // 2-4个标签
		
		for (let i = 0; i < numTags; i++) {
			const tagIndex = (hash + i * 7) % allTags.length
			selectedTags.push(allTags[tagIndex])
		}
		
		return selectedTags
	},

	// 生成医生简介
	generateDoctorIntroduction(doctorName, specialty, hash) {
		const introductions = [
			`${doctorName}医生从事临床工作多年，在${specialty}方面具有丰富的诊疗经验。擅长各种常见病、多发病的诊断和治疗，对疑难杂症有独特的见解。`,
			`${doctorName}医生是${specialty}领域的专家，具有扎实的理论基础和丰富的临床经验。在治疗过程中注重个体化诊疗，深受患者信赖。`,
			`${doctorName}医生专注于${specialty}的研究和治疗，在相关领域发表多篇学术论文。临床经验丰富，技术精湛，是患者值得信赖的医生。`,
			`${doctorName}医生在${specialty}方面造诣深厚，曾多次参加国内外学术交流。擅长运用先进技术进行诊断和治疗，治疗效果显著。`,
			`${doctorName}医生是${specialty}的资深专家，具有丰富的临床经验和深厚的学术造诣。在治疗过程中注重患者体验，深受患者好评。`
		]
		
		return introductions[hash % introductions.length]
	},

	// 生成医生排班
	generateDoctorSchedules(doctorName, hash) {
		const schedules = []
		const dates = [
			{ date: '2020-12-30', day: '周一', timeRelative: '2天后' },
			{ date: '2020-01-02', day: '周三', timeRelative: '4天后' },
			{ date: '2020-01-04', day: '周四', timeRelative: '6天后' },
			{ date: '2020-01-06', day: '周六', timeRelative: '8天后' },
			{ date: '2020-01-08', day: '周一', timeRelative: '10天后' }
		]
		
		dates.forEach((dateInfo, index) => {
			const remaining = this.generateRemaining(hash, index)
			schedules.push({
				...dateInfo,
				fee: 60 + (hash % 20), // 60-80之间的费用
				remaining: remaining,
				available: remaining > 0
			})
		})
		
		return schedules
	},

	// 生成其他医生
	generateOtherDoctors(doctorName, hash) {
		const otherDoctors = []
		const doctorNames = ['刘萌萌', '甄金争', '李丽', '王医生', '张医生', '陈医生']
		const titles = ['主任医师', '副主任医师', '主治医师']
		const specialties = [
			'消化系统疾病治疗，擅长肝胆疾病',
			'胃肠疾病诊治，专长消化内镜',
			'肝胆胰疾病治疗，微创手术专家',
			'消化内科疾病，炎症性肠病专家',
			'消化系统疾病，胃镜检查专家',
			'肝胆疾病治疗，超声介入专家'
		]
		
		for (let i = 0; i < 3; i++) {
			const nameIndex = (hash + i * 11) % doctorNames.length
			const titleIndex = (hash + i * 13) % titles.length
			const specialtyIndex = (hash + i * 17) % specialties.length
			
			otherDoctors.push({
				name: doctorNames[nameIndex],
				title: titles[titleIndex],
				specialty: specialties[specialtyIndex]
			})
		}
		
		return otherDoctors
	},

	// 生成剩余号源
	generateRemaining(hash, index) {
		const random = (hash * 9301 + 49297) % 233280
		const baseRemaining = (random + index * 7) % 20
		const isFull = (random + index * 11) % 10 < 3 // 30%概率已满
		
		return isFull ? 0 : Math.floor(baseRemaining) + 1
	},

	// 简单的哈希函数
	hashCode(str) {
		let hash = 0
		if (str.length === 0) return hash
		for (let i = 0; i < str.length; i++) {
			const char = str.charCodeAt(i)
			hash = ((hash << 5) - hash) + char
			hash = hash & hash
		}
		return Math.abs(hash)
	},

	// 查看其他医生
	viewOtherDoctor(e) {
		const doctor = e.currentTarget.dataset.doctor
		console.log('查看其他医生:', doctor)
		
		// 跳转到其他医生的详情页面
		wx.navigateTo({
			url: `/pages/doctor-detail/doctor-detail?doctor=${doctor.name}&title=${doctor.title}&specialty=${doctor.specialty}&fee=60&department=${this.data.department}&subDepartment=${this.data.subDepartment}`
		})
	},

	// 立即预约
	bookAppointment() {
		console.log('立即预约医生:', this.data.doctor.name)
		
		// 跳转到医生预约页面
		wx.navigateTo({
			url: `/pages/doctor-appointment/doctor-appointment?department=${this.data.department}&subDepartment=${this.data.subDepartment}`
		})
	},

	// 预约特定排班
	bookScheduleAppointment(e) {
		const schedule = e.currentTarget.dataset.schedule
		console.log('预约排班:', schedule)
		
		// 跳转到时间段选择页面
		wx.navigateTo({
			url: `/pages/time-slot/time-slot?doctor=${this.data.doctor.name}&date=${schedule.date}&department=${this.data.department}&subDepartment=${this.data.subDepartment}&campus=${this.data.selectedCampus}`
		})
	},

	onShow() {
		console.log('医生详情页面显示')
	}
})
